<template>
	<view class="u-wrap">
		<view class="u-search-box">
			<u-search placeholder="搜索商品" v-model="keyword" @search="searchGoods" @custom="searchGoods" :focus="false"
				@focus="getFocus"></u-search>
		</view>
		<!--语音搜索-->
		<!--#ifdef APP-PLUS-->
		<view class="addSite">
			<!-- <u-icon name="mic"></u-icon> -->
			<button size="default" class="uni-btn" type="warn" @touchstart="startvoice"
				@touchend="endvoice">按住语话松开停止</button>
		</view>
		<!--#endif-->
	</view>
</template>

<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	export default {
		data() {
			return {
				keyword: ''
			}
		},
		onLoad() {
			this.keyword = this.vuex_keyword
			// #ifdef APP-PLUS
			this.initaudio()
			// #endif
		},
		methods: {
			//语音转文字测试
			async initaudio() {
				//注意此处必须为 await 因为会触发异步事件，手机上会弹出权限申请对话框处理完才能走下一步录音  
				let recordauth = await permision.requestAndroidPermission("android.permission.RECORD_AUDIO")
				console.log("判断有没有录音权限>>>>>>" + recordauth)
				if (recordauth == 1) {
					recorderManager.onStart((res) => {
						console.log("开始 录音>>>>>>>>>...")
					});
					recorderManager.onStop((res) => {
						console.log("recorderstop....res.tempFilePath>>>" + res.tempFilePath)
						this.voicepath = res.tempFilePath
						this.uploadvoicefile()
						// 使用uni.uploadFile上传到服务器上，此时是mp3格式  
					});

					recorderManager.onError((res) => {
						console.log('onError' + JSON.stringify(res));
					});
				}
			},
			// 搜索商品数据
			searchGoods() {
				// 缓存token
				this.$u.vuex('vuex_keyword', this.keyword)
				uni.reLaunch({
					url: "/pages/goods/goods"
				})
			},
			getFocus() {
				console.log('获取了焦点')
			},
			//initaudio 方法结束
			startvoice() {
				console.log("开始录音")
				recorderManager.start({
					format: "mp3",
					sampleRate: 16000 // 必须设置是后台设置的参数，不然百度语音识别不了  
				});
			},
			endvoice() {
				console.log("结束录音")
				//注意为了避免说话时间太短导致这个api出现bug要加一些延时  
				setTimeout(() => {
					recorderManager.stop()
				}, 1000)

			},
			uploadvoicefile() {
				// this.msg = "调用java端服务文件路径"+this.voicepath  
				uni.uploadFile({
					url: 'http://124.221.192.169:996/emos-wx-api/uploadFile',
					filePath: this.voicepath, //录音结束后返回的临时路径，  
					name: 'file',
					formData: {
						dev_id: 1537 //中文带标点  
					},
					success: (uploadFileRes) => {
						//console.log(uploadFileRes.data)
						const result = JSON.parse(uploadFileRes.data)
						//console.log(result)
						this.keyword = result.msg
						this.keyword = this.keyword.replace(
							/[`:_.~!@#$%^&*() \+ =<>?"{}|, \/ ;' \\ [ \] ·~！@#￥%……&*（）—— \+ ={}|《》？：“”【】、；‘’，。、]/g,
							'');
						console.log("上传音频成功" + this.keyword);
					},
					fail: (res) => {
						console.log("上传音频失败" + JSON.stringify(res));
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		/* #ifdef H5*/
		height: calc(100vh - var(--window-top) - 100rpx);
		/* #endif*/
		/* #ifndef H5*/
		height: 100vh;
		/* #endif*/
		display: flex;
		flex-direction: column;

		.loading {
			text-align: center;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
		}
	}

	.u-search-box {
		//padding: 18rpx 30rpx;
		border: 2px solid #EEEEEE;
		color: #BDBDBD;
		background-color: #FFFFFF;
		border-radius: 8px;
		margin: 10rpx;

		.icon {
			margin-left: 10rpx;
			margin-right: 20rpx;
			width: 40rpx;
			display: inline-block;
		}

		.search-word {
			display: inline;
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 260rpx;
		position: absolute;
		bottom: 30rpx;
		left: 80rpx;
		background-color: #ffffff;
		border-radius: 60rpx;
		font-size: 30rpx;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>
